﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Net5ApiAndEfMySql.API.Common;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.IService;
using Net5ApiAndEfMySql.IService.SystemAdmin;
using Net5ApiAndEfMySql.Repository;
using Net5ApiAndEfMySql.Repository.SystemAdmin;
using Net5ApiAndEfMySql.Service;
using Net5ApiAndEfMySql.Service.SystemAdmin;
using Net5CommonLib.SystemLib.Common;
using VueAndNetCoreEfApi.IRepository.SystemAdmin;
using VueAndNetCoreEfApi.IService.SystemAdmin;
using VueAndNetCoreEfApi.Repository.SystemAdmin;
using VueAndNetCoreEfApi.Service.SystemAdmin;

namespace VueAndNetCoreEfApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        private readonly string MyAllowSpecificOrigins = "CorsPolicy";
        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            //ע��Http�����Ĺ��팦��
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            //����Ef���Ӷ���ע��
            services.AddDbContext<SoaSystemCenterDbContext>(
                options =>
                    options.UseMySql(connectionString: Configuration.GetSection("ConnectionStrings")["SoaSystemCenterMysqlConnection"],
                        serverVersion: new MySqlServerVersion(new Version(5, 6, 31))
                    )
            );
            //����
            services.AddCors(options => options.AddPolicy(MyAllowSpecificOrigins, builder =>
                {
                    builder.WithOrigins("urls")
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .SetIsOriginAllowed(_ => true)
                        .AllowCredentials();
                })
            );

            services.AddScoped<IOperatorService, OperatorService>();
            services.AddScoped<IOperatorRepository, OperatorRepository>();

            services.AddScoped<IRoleDbService, RoleDbService>();
            services.AddScoped<IRoleDbRepository, RoleDbRepository>();

            services.AddScoped<IPositioninfoRepository, PositioninfoRepository>();
            services.AddScoped<IPositioninfoService, PositioninfoService>();

            services.AddScoped<IOwnerService, OwnerService>();
            services.AddScoped<IOwnerRepository, OwnerRepository>();

            services.AddScoped<IdepartmentService, departmentService>();
            services.AddScoped<IdepartmentRespository, departmentRespository>();

            services.AddScoped<IBaseService, BaseService>();
            //������
            services.AddScoped<UserTokenVerificationAttribute>();

            services.AddScoped<IOutsourceRepository, OutsourceRepository>();
            services.AddScoped<IOutsourceService, OutsourceService>();
            services.AddScoped<ILineRepository, LineRepository>();
            services.AddScoped<ILineService, LineService>();
            //redis配置
            //redis����
            var csredis = new CSRedis.CSRedisClient(Configuration.GetSection("ConnectionStrings")["DefaultRedisConnection"]);
            RedisHelper.Initialization(csredis);//��ʼ��

        }

        //�˷���������ʱ���á�ʹ�ô˷�������HTTP�����ܵ�.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            DiManager.Instance = app.ApplicationServices; //��ע����������DiManager
            //�Ƿ��ǿ�������
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();//�쳣/��������
            }
            //����
            app.UseCors(MyAllowSpecificOrigins);
            //����·��������·���м���
            app.UseRouting();
            //��̬�ļ��м��� 
            app.UseStaticFiles();
            //������Ȩ�û����ʰ�ȫ��Դ����Ȩ
            app.UseAuthorization();
            //�ս������ӵ������ܵ����ս���·���м���
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                //endpoints.MapControllers().RequireCors(MyAllowSpecificOrigins);
            });
        }
    }
}
